package com.util.download.core;

import com.util.download.constant.Constant;

/**
 * @Classname DownloadInfoThread
 * @Description TODO
 * @Date 2023/11/7 20:26
 * @Created by wangxianlin
 */
public class DownloadInfoThread implements Runnable{

    /**
     * 下载文件的总的大小
     */
    public long httpFileContentLength;

    /**
     * 本地已下载文件的大小
     */
    public double finishedSize;

    /**
     * 本次累计下载的大小
     */
    public volatile double downSize;

    /**
     * 前一次下载的大小
     */
    public double preSize;

    public DownloadInfoThread(long httpFileContentLength) {
        this.httpFileContentLength = httpFileContentLength;
    }

    @Override
    public void run() {
        // 计算文件的总大小,单位是MB
        String httpFileSize = String.format("%.2f",httpFileContentLength/ Constant.MB);

        // 计算每秒的下载速度 kb
        int speed =(int) ((downSize - preSize) / 1024d);

        preSize = downSize;
        // 剩余文件的总大小
        double remainSize = httpFileContentLength - finishedSize - downSize;

        // 计算剩余时间
        String remainTime =  String.format("%.1f",remainSize/1024d/speed);

        if("Infinity".equalsIgnoreCase(remainTime)){
            remainTime = "-";
        }
        // 已下载的大小
        String currentFileSize = String.format("%.1f",(downSize - finishedSize)/Constant.MB);
        String downloadInfo = String.format(" 已下载 %smb/%smb,速度： %skb/s，剩余时间：%ss",currentFileSize,httpFileSize,speed,remainSize);
        System.out.println(downloadInfo);
    }
}
